﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.PM;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.IO.Compression;
using System.Net;
using System.Data.OleDb;
using System.Collections;
using System.Reflection;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace SMALLERP.PM
{
    
     
    public partial class WLZXQ : Form
    {
        private readonly DataBase db = new DataBase();
        List<int> _indexlist = new List<int>();
        private SqlDataReader sdr1;
        public WLZXQ()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string strSql = null;
            if (checkBox1.CheckState == CheckState.Checked)
            {

                //strSql = "DELETE FROM MOCTY WHERE ISNULL(COMPANY,'')<>''";
                       
                //        //MessageBox.Show(strSql,"提示");
                //        try
                //        {
                //            if (db.ExecDataBySql1(strSql) > 0)
                //            {
                                
                //            }
                //            else
                //            {
                //                //MessageBox.Show("保存失败！", "软体提示");
                //                //return;
                //            }
                //        }

                //        catch (Exception ex)
                //        {
                //            MessageBox.Show(ex.Message, "软件提示");
                //            throw ex;
                //        }

                       strSql = "EXEC sp_wlxq";
                       
                        //MessageBox.Show(strSql,"提示");
                        try
                        {
                            if (db.ExecDataBySql1(strSql) > 0)
                            {
                                
                            }
                            else
                            {
                                //MessageBox.Show("保存失败！", "软体提示");
                                //return;
                            }
                        }

                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "软件提示");
                            throw ex;
                        }
            }
            
            
            gridControl1.DataSource = "";
            gridControl1.Refresh();
            
            if (checkBox2.CheckState == CheckState.Checked)
            {
                strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select PLANT_NAME 工厂,RTRIM(A.ID) 品号,A.MB002 品名,A.MB003 规格,A.MB017 主要仓库,RTRIM(ISNULL(SUPPLIER_NAME,'''')) 主要供应商,CEILING(A.MB064) 总库存,0.00 AS 总需求,0 AS 物料平衡状况,CAST(ISNULL(MC004,0.00) AS REAL) AS 安全库存,CEILING(ZCGL) 已购未回量,CEILING(ZSCL) 总生产量,A.LB 类别' select @sql1 = @sql1 + ' , max(case XQRQ when ''' + XQRQ + ''' then XQL else 0 end) [' + XQRQ + ']' from (select DISTINCT XQRQ from V_XQRQ WHERE (XQRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox4.Text + "'),112) OR '" + textBox4.Text + "'='') AND (XQRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox5.Text + "'),112) OR '" + textBox5.Text + "'='')) as a set @sql1 = @sql1 + ' from V_WLZXQ A LEFT JOIN ITEM ON ID=ITEM_CODE LEFT JOIN ITEM_PLANT ON ITEM.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID LEFT JOIN PLANT ON A.Owner_Org_ROid=PLANT_ID LEFT JOIN ITEM_PURCHASE ON ITEM_PURCHASE.ITEM_ID=ITEM.ITEM_BUSINESS_ID AND ITEM_PURCHASE.Owner_Org_ROid=''C9AA0C13-9ECE-47C2-7271-1712B78C6B2B'' LEFT JOIN ITEM_SUPPLIER ON ITEM_PURCHASE.ITEM_BUSINESS_ID=ITEM_SUPPLIER.ITEM_BUSINESS_ID AND ITEM_SUPPLIER.MAIN=''1'' LEFT JOIN SUPPLIER ON SUPPLIER.SUPPLIER_BUSINESS_ID=ITEM_SUPPLIER.SUPPLIER_ID LEFT JOIN EMPLOYEE ON EMPLOYEE_ID=BUYER_ID LEFT JOIN WORK_CENTER ON ITEM_PLANT.WORK_CENTER_ID=WORK_CENTER.WORK_CENTER_ID LEFT JOIN WAREHOUSE ON WAREHOUSE.WAREHOUSE_ID=ITEM_PLANT.OUTBOUND_WAREHOUSE_ID WHERE (WORK_CENTER.WORK_CENTER_CODE LIKE ''" + textBox1.Text + "%'' OR ''" + textBox1.Text + "''='''') AND (ITEM_PLANT.ITEM_PROPERTY=''" + comboBox1.Text + "'' OR ''" + comboBox1.Text + "''='''') AND ( A.ID like ''" + textBox2.Text + "%'' OR ''" + textBox2.Text + "''='''')";
                strSql = strSql + " AND (ISNULL(EMPLOYEE_CODE,'''')=''" + textBox3.Text + "'' OR ISNULL(EMPLOYEE_NAME,'''') LIKE ''%" + textBox3.Text + "%'') AND (ISNULL(WAREHOUSE_CODE,'''')=''" + textBox11.Text + "'' OR ''" + textBox11.Text + "''='''') AND (A.MB002 LIKE ''%" + textBox7.Text + "%'' OR A.MB003 LIKE ''%" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.TC012 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND (EXISTS(SELECT * FROM PURCHASE_ORDER_SD LEFT JOIN PURCHASE_ORDER_D ON PURCHASE_ORDER_SD.PURCHASE_ORDER_D_ID=PURCHASE_ORDER_D.PURCHASE_ORDER_D_ID LEFT JOIN PURCHASE_ORDER ON PURCHASE_ORDER_D.PURCHASE_ORDER_ID=PURCHASE_ORDER.PURCHASE_ORDER_ID LEFT JOIN SUPPLIER ON SUPPLIER.SUPPLIER_BUSINESS_ID=PURCHASE_ORDER.SUPPLIER_ID WHERE PURCHASE_ORDER_SD.RECEIPT_CLOSE=''0'' AND PURCHASE_ORDER_D.ITEM_ID=ITEM.ITEM_BUSINESS_ID AND SUPPLIER_CODE=''" + textBox10.Text + "'') OR ''" + textBox10.Text + "''='''')) AND (('" + PropertyClass.jiaose + "'='供应商' AND SUPPLIER_CODE=''" + PropertyClass.gzzx + "'') OR ''" + PropertyClass.jiaose + "''<>''供应商'') AND EXISTS(SELECT * FROM MO_D LEFT JOIN MO ON MO_D.MO_ID=MO.MO_ID LEFT JOIN ITEM ON MO_D.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN WORK_CENTER ON MO.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID WHERE ITEM.ITEM_CODE=A.ID AND WORK_CENTER.WORK_CENTER_CODE LIKE ''%" + textBox9.Text + "%'')  group by A.ID,A.MB002,A.MB003,MB017,A.MB064,ZCGL,ZSCL,A.LB,MC004,PLANT_NAME,ISNULL(SUPPLIER_NAME,'''') ORDER BY A.ID,A.LB DESC' EXEC(@sql1)";
                //textBox1.Text = strSql;
                //return;
                try
                {
                    DataTable dt = db.GetDataSet1(strSql, "PURTZ").Tables["PURTZ"]; ;
                    DataView view = new DataView();
                    view.Table = dt;
                    view.RowFilter = "工厂 LIKE '%" + comboBox2.Text + "%'";//itemType是A中的一个字段
                    gridControl1.DataSource = view.ToTable();
                    this.gridView1.Columns["工厂"].Visible = false;
                    ////this.dataGridView1.Columns["适用产品"].Width = 120;
                    //this.dataGridView1.Columns["品名"].Width = 50;
                    //this.dataGridView1.Columns["规格"].Width = 70;
                    //this.dataGridView1.Columns["总库存"].Width = 50;
                    //this.dataGridView1.Columns["总需求"].Width = 50;
                    //this.dataGridView1.Columns["物料平衡状况"].Width = 50;
                    //this.dataGridView1.Columns["安全库存"].Width = 50;
                    //this.dataGridView1.Columns["已购未回量"].Width = 50;
                    //this.dataGridView1.Columns["总生产量"].Width = 50;
                    for (int i = this.gridView1.Columns["总生产量"].VisibleIndex; i <= this.gridView1.Columns.Count - 1; i++)
                    {
                        this.gridView1.Columns[i].Width = 60;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }

            if (checkBox2.CheckState == CheckState.Unchecked)
            {
                strSql = "declare @sql1 VARCHAR(8000) set @sql1 = 'select PLANT_NAME 工厂,RTRIM(A.ID) 品号,A.MB002 品名,A.MB003 规格,A.MB017 主要仓库,RTRIM(ISNULL(SUPPLIER_NAME,'''')) 主要供应商,CEILING(A.MB064) 总库存,0.00 AS 总需求,0 AS 物料平衡状况,CAST(ISNULL(MC004,0.00) AS REAL) AS 安全库存,CEILING(ZCGL) 已购未回量,CEILING(ZSCL) 总生产量' select @sql1 = @sql1 + ' , max(case XQRQ when ''' + XQRQ + ''' then CEILING(XQL) else 0 end) [' + XQRQ + ']' from (select DISTINCT XQRQ from V_XQRQ WHERE (XQRQ>=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox4.Text + "'),112) OR '" + textBox4.Text + "'='') AND (XQRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox5.Text + "'),112) OR '" + textBox5.Text + "'='')) as a set @sql1 = @sql1 + ' from V_WLZXQ A LEFT JOIN ITEM ON ID=ITEM_CODE LEFT JOIN ITEM_PLANT ON ITEM.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID LEFT JOIN PLANT ON A.Owner_Org_ROid=PLANT_ID LEFT JOIN ITEM_PURCHASE ON ITEM_PURCHASE.ITEM_ID=ITEM.ITEM_BUSINESS_ID AND ITEM_PURCHASE.Owner_Org_ROid=''C9AA0C13-9ECE-47C2-7271-1712B78C6B2B'' LEFT JOIN ITEM_SUPPLIER ON ITEM_PURCHASE.ITEM_BUSINESS_ID=ITEM_SUPPLIER.ITEM_BUSINESS_ID AND ITEM_SUPPLIER.MAIN=''1'' LEFT JOIN SUPPLIER ON SUPPLIER.SUPPLIER_BUSINESS_ID=ITEM_SUPPLIER.SUPPLIER_ID LEFT JOIN EMPLOYEE ON EMPLOYEE_ID=BUYER_ID LEFT JOIN WORK_CENTER ON ITEM_PLANT.WORK_CENTER_ID=WORK_CENTER.WORK_CENTER_ID LEFT JOIN WAREHOUSE ON WAREHOUSE.WAREHOUSE_ID=ITEM_PLANT.OUTBOUND_WAREHOUSE_ID WHERE LB=''需求'' AND (WORK_CENTER.WORK_CENTER_CODE LIKE ''" + textBox1.Text + "%'' OR ''" + textBox1.Text + "''='''') AND (ITEM_PLANT.ITEM_PROPERTY=''" + comboBox1.Text + "'' OR ''" + comboBox1.Text + "''='''') AND ( A.ID like ''" + textBox2.Text + "%'' OR ''" + textBox2.Text + "''='''')";
                strSql = strSql + " AND (ISNULL(EMPLOYEE_CODE,'''')=''" + textBox3.Text + "'' OR ISNULL(EMPLOYEE_NAME,'''') LIKE ''%" + textBox3.Text + "%'')  AND ((ISNULL(WAREHOUSE_CODE,'''')=''" + textBox11.Text + "'' OR ''" + textBox11.Text + "''='''') AND (A.MB002 LIKE ''%" + textBox7.Text + "%'' OR A.MB003 LIKE ''%" + textBox7.Text + "%'' OR ''" + textBox7.Text + "''='''') AND (A.TC012 LIKE ''%" + textBox8.Text + "%'' OR ''" + textBox8.Text + "''='''') AND (EXISTS(SELECT * FROM PURCHASE_ORDER_SD LEFT JOIN PURCHASE_ORDER_D ON PURCHASE_ORDER_SD.PURCHASE_ORDER_D_ID=PURCHASE_ORDER_D.PURCHASE_ORDER_D_ID LEFT JOIN PURCHASE_ORDER ON PURCHASE_ORDER_D.PURCHASE_ORDER_ID=PURCHASE_ORDER.PURCHASE_ORDER_ID LEFT JOIN SUPPLIER ON SUPPLIER.SUPPLIER_BUSINESS_ID=PURCHASE_ORDER.SUPPLIER_ID WHERE PURCHASE_ORDER_SD.RECEIPT_CLOSE=''0'' AND PURCHASE_ORDER_D.ITEM_ID=ITEM.ITEM_BUSINESS_ID AND SUPPLIER_CODE=''" + textBox10.Text + "'') OR ''" + textBox10.Text + "''='''')) AND LB=''需求'' AND ((''" + PropertyClass.jiaose + "''=''供应商'' AND SUPPLIER_CODE=''" + PropertyClass.gzzx + "'') OR ''" + PropertyClass.jiaose + "''<>''供应商'') AND EXISTS(SELECT * FROM MO_D LEFT JOIN MO ON MO_D.MO_ID=MO.MO_ID LEFT JOIN ITEM ON MO_D.ITEM_ID=ITEM.ITEM_BUSINESS_ID LEFT JOIN WORK_CENTER ON MO.SOURCE_ID_ROid=WORK_CENTER.WORK_CENTER_ID WHERE ITEM.ITEM_CODE=A.ID AND WORK_CENTER.WORK_CENTER_CODE LIKE ''%" + textBox9.Text + "%'') group by A.ID,A.MB002,A.MB003,A.MB017,A.MB064,ZCGL,ZSCL,MC004,PLANT_NAME,ISNULL(SUPPLIER_NAME,'''') ORDER BY A.ID' EXEC(@sql1)";
                //textBox1.Text = strSql;
                //return;
                try
                {
                    DataTable dt= db.GetDataSet1(strSql, "PURTZ").Tables["PURTZ"]; ;
                    DataView view = new DataView();
                    view.Table = dt;
                    view.RowFilter = "工厂 LIKE '%" + comboBox2.Text + "%'";//itemType是A中的一个字段
                    gridControl1.DataSource = view.ToTable();
                    gridView1.PopulateColumns();

                    //gridControl1.DataSource = dt;
                    //this.dataGridView1.Columns["工厂"].Visible = false;
                    ////this.dataGridView1.Columns["适用产品"].Width = 120;
                    //this.dataGridView1.Columns["品名"].Width = 50;
                    //this.dataGridView1.Columns["规格"].Width = 70;
                    //this.dataGridView1.Columns["总库存"].Width = 50;
                    //this.dataGridView1.Columns["总需求"].Width = 50;
                    //this.dataGridView1.Columns["物料平衡状况"].Width = 50;
                    //this.dataGridView1.Columns["安全库存"].Width = 50;
                    //this.dataGridView1.Columns["已购未回量"].Width = 50;
                    //this.dataGridView1.Columns["总生产量"].Width = 50;
                    //for (int i = this.gridView1.Columns["总生产量"].VisibleIndex + 1; i <= this.gridView1.Columns.Count-1; i++)
                    //{
                    //    this.gridView1.Columns[i].Width = 60;
                    //}
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "软件提示");
                    throw ex;
                }
            }
         
            if (gridView1.RowCount - 1 >= 0)
            {
                double wlph;
                progressBar1.Visible = true;
                progressBar1.Maximum = gridView1.RowCount;//设置最大长度值
                progressBar1.Value = 0;//设置当前值
                progressBar1.Step = 1;//设置没次增长多少
                for (int i = 0; i <= gridView1.RowCount - 1; i++)
                {
                    progressBar1.Value += progressBar1.Step;
                    strSql = "SELECT CEILING(ISNULL(SUM(XQL),0.00)) 总需求 FROM WLXQ WHERE LB='需求' AND ID='" + this.gridView1.GetRowCellValue(i,"品号").ToString() + "' AND XQRQ<=CONVERT(CHAR(8),DATEADD(DAY,0,'" + textBox5.Text + "'),112)";
                    //MessageBox.Show(strSql,"提示");
                    try
                    {
                        sdr1 = db.GetDataReader1(strSql);

                        while (sdr1.Read())
                        {
                            this.gridView1.SetRowCellValue(i, "总需求", Convert.ToDouble(sdr1["总需求"]).ToString());
                            wlph = Convert.ToDouble(this.gridView1.GetRowCellValue(i, "总库存").ToString()) + Convert.ToDouble(this.gridView1.GetRowCellValue(i, "已购未回量").ToString()) + Convert.ToDouble(this.gridView1.GetRowCellValue(i, "总生产量").ToString()) - Convert.ToDouble(this.gridView1.GetRowCellValue(i, "总需求").ToString());
                            this.gridView1.SetRowCellValue(i, "物料平衡状况", wlph);
                            //this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总需求"].Index].Value = Convert.ToDouble(sdr1["总需求"]).ToString();
                            //wlph = Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总库存"].Index].Value) + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["已购未回量"].Index].Value) + Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总生产量"].Index].Value) - Convert.ToDouble(this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["总需求"].Index].Value);
                            //this.dataGridView1.Rows[i].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value = wlph;


                        }

                        if (sdr1.HasRows == false)
                        {
                            this.gridView1.SetRowCellValue(i, "物料平衡状况", "0");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "软件提示");
                        //throw ex;
                    }
                    finally
                    {
                        sdr1.Close();
                    }

                }
                progressBar1.Visible = false;
            }
            GridFormatRule gfrId = new GridFormatRule();
            gfrId.Rule = new FormatConditionRuleExpression()
            {
                //使用Dev默认提供的预定义样式
                PredefinedName = "Red Fill,Red Text",
                Expression = "[物料平衡状况]<0",
            };
            gfrId.ApplyToRow = true;
            gridView1.FormatRules.Add(gfrId);

        }

        private void WLZXQ_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now.AddDays(-1).Date;
            dateTimePicker2.Value = DateTime.Now.AddDays(30).Date;
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //try
            //{
            //    if (checkBox2.CheckState == CheckState.Unchecked)
            //    {
            //        if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["物料平衡状况"].Index)
            //        {
            //            if (e.Value == DBNull.Value)
            //            {
            //                e.Value = "0";
            //            }
            //            //string sl = (e.Value == null) ? "0.00" : e.Value.ToString();
            //            //e.Value = sl.ToString().TrimEnd('0').TrimEnd('.');
            //        }
            //        string wlph = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value == null) ? "0.00" : this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value.ToString();
            //        if (Convert.ToDouble(wlph) < 0.00)
            //        {

            //            for (int i = 0; i <= this.dataGridView1.Columns["总生产量"].Index; i++)
            //            {
            //                this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Red;
            //            }
            //        }

            //        if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["总生产量"].Index)
            //        {
            //            double ljxq = 0.00;

            //            for (int i = this.dataGridView1.Columns["总生产量"].Index + 1; i <= e.ColumnIndex; i++)
            //            {

            //                string pcsl = (this.dataGridView1.Rows[e.RowIndex].Cells[i].Value == null) ? "0.00" : this.dataGridView1.Rows[e.RowIndex].Cells[i].Value.ToString();
            //                //pcsl = (e.Value == "") ? "0.00" : e.Value.ToString();
            //                ljxq = ljxq + Convert.ToDouble(pcsl);

            //            }
            //            if (Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) != 0.00 && ljxq > Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["总库存"].Index].Value))
            //            {
            //                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
            //                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
            //            }
            //        }
            //    }

            //    if (checkBox2.CheckState == CheckState.Checked)
            //    {
            //        if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["规格"].Index)
            //        {
            //            if (e.Value == DBNull.Value)
            //            {
            //                e.Value = "0";
            //            }
            //            string sl = (e.Value == null) ? "0.00" : e.Value.ToString();
            //            e.Value = sl.ToString().TrimEnd('0').TrimEnd('.');
            //        }
            //        if (Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["总生产量"].Index + 1].Value) == "供给")
            //        {
            //            if (e.RowIndex > -1 && e.ColumnIndex <= this.dataGridView1.Columns["总生产量"].Index)
            //            {
            //                e.Value = "";
            //            }
            //        }
            //        double wlph = (this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value == null) ? 0.00 : Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["物料平衡状况"].Index].Value);
            //        if (Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["总生产量"].Index + 1].Value) == "需求")
            //        {
            //            if (wlph < 0.00)
            //            {
            //                for (int i = 0; i <= this.dataGridView1.Columns["总生产量"].Index + 1; i++)
            //                {
            //                    this.dataGridView1.Rows[e.RowIndex].Cells[i].Style.BackColor = Color.Red;
            //                }
            //            }
            //        }

            //        if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["总生产量"].Index + 1 && Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["类别"].Index].Value) == "需求")
            //        {
            //            double ljxq = 0.00;
            //            for (int i = this.dataGridView1.Columns["总生产量"].Index + 2; i <= e.ColumnIndex; i++)
            //            {
            //                ljxq = ljxq + Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[i].Value);

            //            }
            //            if (Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) != 0.00 && ljxq > Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["总库存"].Index].Value))
            //            {
            //                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
            //                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.ForeColor = Color.Red;
            //            }
            //        }

            //        if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["总生产量"].Index + 1 && Convert.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[this.dataGridView1.Columns["类别"].Index].Value) == "供给")
            //        {
            //            double ljxq = 0.00;
            //            double ljgj = 0.00;
            //            for (int i = this.dataGridView1.Columns["总生产量"].Index + 2; i <= e.ColumnIndex; i++)
            //            {
            //                ljxq = ljxq + Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex - 1].Cells[i].Value);
            //                ljgj = ljgj + Convert.ToDouble(this.dataGridView1.Rows[e.RowIndex].Cells[i].Value);
            //            }
            //            if (ljxq > ljgj)
            //            {
            //                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Blue;

            //            }
            //        }
            //    }
            //}
            //catch
            //{ 
            
            //}

        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //string x1 = "";
            //string x2 = "";
            //double x3 = 0.00;
            //string x4="";
            //string x5 = "";
            //x1 = dataGridView1["品号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
            //x2 = this.dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name.ToString();
            //x3 = Convert.ToDouble(this.dataGridView1.CurrentCell.Value.ToString());

            //if (e.RowIndex > -1 && e.ColumnIndex==this.dataGridView1.Columns["已购未回量"].Index)
            //{
            //    x4 = "查看采购订单";
                
            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["总库存"].Index)
            //{
            //    x4 = "查看库存";
                
            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["总生产量"].Index)
            //{
            //    x4 = "查看工单";
                
            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["总生产量"].Index)
            //{
            //    x4 = "查看需求";
               
            //}
            //else
            //{
            //    x4 = "其他";
            //    MessageBox.Show("本列无明细数据", "软件提示");
            //}

            //if (x4 != "其他")
            //{
            //    MXCX xx = new MXCX(x1, x2, x3, x4,x5);
            //    CHAXUN f = new CHAXUN();
            //    f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
            //    f.Show(this);
            //}
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            
        }

        private void 查看累计需求ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0.00;
            string x4 = "";
            string x5 = "";
            x1 = gridView1.GetFocusedRowCellValue("品号").ToString();
            x2 = gridView1.FocusedColumn.FieldName.ToString();
            x3 = Convert.ToDouble(gridView1.GetFocusedValue());
           // MessageBox.Show(x2.ToString(),"系统提示");
            if (gridView1.FocusedRowHandle> -1 && gridView1.FocusedColumn.ColumnHandle == this.gridView1.Columns["已购未回量"].ColumnHandle)
            {
                x4 = "查看采购订单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.ColumnHandle == this.gridView1.Columns["总库存"].ColumnHandle)
            {
                x4 = "查看库存";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.ColumnHandle == this.gridView1.Columns["总生产量"].ColumnHandle)
            {
                x4 = "查看工单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.ColumnHandle > this.gridView1.Columns["总生产量"].ColumnHandle)
            {
                x4 = "查看需求";

            }
            else
            {
                x4 = "其他";
                MessageBox.Show("本列无明细数据", "软件提示");
            }

            if (x4 != "其他")
            {
                MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                CHAXUN f = new CHAXUN();
                f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            //if (e.RowIndex > -1 && e.ColumnIndex > this.dataGridView1.Columns["总生产量"].Index)
            //{
            //    if (e.Button == MouseButtons.Right)
            //    {
            //        if (e.RowIndex >= 0)
            //        {
            //            ////若行已是选中状态就不再进行设置
            //            //if (dataGridView1.Rows[e.RowIndex].Selected == false)
            //            //{
            //            //    dataGridView1.ClearSelection();
            //            //    dataGridView1.Rows[e.RowIndex].Selected = true;
            //            //}
            //            ////只选中一行时设置活动单元格
            //            //if (dataGridView1.SelectedRows.Count == 1)
            //            //{
            //            //    dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            //            //}
            //            ////弹出操作菜单
            //            contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
            //        }
            //    }
            //}
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            textBox4.Text = dateTimePicker1.Value.ToShortDateString();
            if (textBox5.Text != "")
            {
                TimeSpan ts = Convert.ToDateTime(textBox5.Text) - Convert.ToDateTime(textBox4.Text);
                int k = ts.Days;
                if (k >= 0)
                {
                    //DataTable dt = new DataTable();//建立个数据表

                    //DateTime rq = Convert.ToDateTime(textBox1.Text);

                    //for (int i = 0; i <= k; i++)
                    //{
                    //   rq = Convert.ToDateTime(textBox1.Text).AddDays(i);
                    //  string str = rq.ToString("MM月dd日");
                    //MessageBox.Show("相差" + str + "天", "信息");
                    // dt.Columns.Add(new DataColumn(Convert.ToString(str), typeof(string)));
                    //}
                    //dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("起始日期不能小于截止日期", "提示");
                    textBox4.Text = textBox5.Text;
                }
            }

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            textBox5.Text = dateTimePicker2.Value.ToShortDateString();
            if (textBox4.Text != "")
            {
                TimeSpan ts = Convert.ToDateTime(textBox5.Text) - Convert.ToDateTime(textBox4.Text);
                int k = ts.Days;
                if (k >= 0)
                {
                    //DataTable dt = new DataTable();//建立个数据表

                    //DateTime rq = Convert.ToDateTime(textBox1.Text);

                    //for (int i = 0; i <= k; i++)
                    //{
                    //    rq = Convert.ToDateTime(textBox1.Text).AddDays(i);
                    //    string str = rq.ToString("MM月dd日");
                    //    //MessageBox.Show("相差" + str + "天", "信息");
                    //    dt.Columns.Add(new DataColumn(Convert.ToString(str), typeof(string)));
                    //}
                    //dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("起始日期不能小于截止日期", "提示");
                    textBox5.Text = textBox4.Text;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            string wlph = gridView1.GetRowCellValue(e.RowHandle,"物料平衡状况").ToString();
            if (Convert.ToDouble(wlph) < 0.00)
            {
               if(e.Column.VisibleIndex<= gridView1.Columns["总生产量"].VisibleIndex)
                {
                    e.Appearance.BackColor = Color.Red;
                }
            }

            if (e.RowHandle > -1 && e.Column.ColumnHandle >= this.gridView1.Columns["总生产量"].ColumnHandle + 1)
            {
                double ljxq = 0.00;
                for (int i = this.gridView1.Columns["总生产量"].ColumnHandle + 1; i <= e.Column.ColumnHandle; i++)
                {
                    ljxq = ljxq + Convert.ToDouble(gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns[i].FieldName).ToString());
                }
             
                if (Convert.ToDouble(this.gridView1.GetRowCellValue(e.RowHandle, e.Column).ToString()) != 0.00 && ljxq > Convert.ToDouble(gridView1.GetRowCellValue(e.RowHandle, "总库存").ToString()))
                {
                    e.Appearance.BackColor = Color.Yellow;
                    e.Appearance.ForeColor = Color.Red;
                }
            }
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
           
        }

        private void gridControl1_Enter(object sender, EventArgs e)
        {

        }

        private void gridView1_GotFocus(object sender, EventArgs e)
        {
         
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {

        }

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
           
        }

        private void gridView1_CustomRowFilter(object sender, DevExpress.XtraGrid.Views.Base.RowFilterEventArgs e)
        {
            if (_indexlist.Count == 0)
                return;

            if (_indexlist.Contains(-2))
                e.Visible = true;   //全显示
            else if (_indexlist.Contains(-3))
                e.Visible = false;   //全隐藏
            else if (_indexlist.Contains(e.ListSourceRow))
                e.Visible = true;
            else
                e.Visible = false;

            e.Handled = true;
        }

        private void 仅显示当天缺料ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable table = gridControl1.DataSource as DataTable;
            if (table == null)
                return;

            _indexlist.Clear();

            for (int i = 0; i < table.Rows.Count; i++)
            {
                double ljxq = 0.00;
                for (int j = this.gridView1.Columns["总生产量"].ColumnHandle + 1; j <= this.gridView1.FocusedColumn.ColumnHandle; j++)
                {
                   
                    ljxq = ljxq + Convert.ToDouble(gridView1.GetRowCellValue(i, gridView1.Columns[j].FieldName).ToString());
                }

                if (Convert.ToDouble(this.gridView1.GetRowCellValue(i, this.gridView1.FocusedColumn.FieldName).ToString()) != 0.00 && ljxq > Convert.ToDouble(gridView1.GetRowCellValue(i, "总库存").ToString()))
                {
                    //MessageBox.Show(i.ToString(),"提示");
                    _indexlist.Add(i);
                }
            }
            gridView1.RefreshData();


            //if (_indexlist.Count == 0)
            // HideGridView();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            string rowCellmsg = string.Empty;
            GridViewInfo info = gridView1.GetViewInfo() as GridViewInfo;
            GridCellInfo cellInfo = info.GetGridCellInfo(gridView1.FocusedRowHandle, gridView1.FocusedColumn);

            if (cellInfo == null) return;

            if (cellInfo.CellValue == null) Clipboard.Clear();　　//可能会产生null抛出异常,故需要清空剪贴板信息
            else
                rowCellmsg = cellInfo.CellValue.ToString();

            if (string.IsNullOrEmpty(rowCellmsg))　　　　　　　　　　//单元格内容为空的时候,不能复制为空消息,故需要清空剪贴板的信息
            {
                Clipboard.Clear();
            }
            else
            {
                Clipboard.SetText(rowCellmsg);
            }
        }

        private void 显示全部ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _indexlist.Add(-2);
            gridView1.RefreshData();
        }
    }
}
